package ma.view.gui.parameterPanels;

import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import ma.controller.panels.auxillary.GeometryPanelListener;
import ma.controller.panels.auxillary.CtrInformationPanel;
import ma.controller.panels.parameterpanels.CtrGeoPanel;
import ma.model.ManufacturingMethodInterface;
import ma.view.gui.CenterTabbedPane.EBenchmarkTab;

/**
 * The {@code GeometryPanel} class shows all geometry parameters
 * that are set by the user. The user sets the number of geometry sets.
 * @author Oliver Eckstein
 *
 */
public abstract class GeometryPanel 
    extends InputPanel {

    private static final long serialVersionUID = 1L;
    
    private ManufacturingMethodInterface manuMethod;
    private JButton plusButton;
    private JButton minusButton;
    private JLabel statusMessageLabel;    
    protected JPanel buttonPanel;
    private CtrInformationPanel infoPanelCtr;
    private String standardMessage = "Anzahl an Geometriesets einstellen: ";
    
    /**
     * To create a new instance of a {@code GeometryParameterPanelsPanel}. 
     * @param geometrySets the geometry sets whose panels get created.
     */
    public GeometryPanel(
            int startGeometryCount,
            String logoPath,
            String manuMethodName,
            ManufacturingMethodInterface manuMethod,
            CtrInformationPanel infoPanelCtr,
            CtrGeoPanel geometryController) {
        super(logoPath, manuMethodName);
        this.manuMethod = manuMethod;
        this.infoPanelCtr = infoPanelCtr;
        addConfigurationPanel(geometryController);        
        statusMessageLabel.setText(standardMessage + startGeometryCount);
        this.putClientProperty("ident", EBenchmarkTab.GEOMETRY_PARAMETERS);
    }
   
    /**
     * To add the panel above the geometry panels.
     */
    private void addConfigurationPanel(CtrGeoPanel geometryController) {        
        statusMessageLabel = new JLabel(standardMessage);
        buttonPanel.add(statusMessageLabel);
        plusButton = new JButton("+");
        plusButton.setPreferredSize(new Dimension(50, 20));
        plusButton.addActionListener(new GeometryPanelListener(
                "+",
                manuMethod,
                this,
                infoPanelCtr, geometryController));
        minusButton = new JButton("-");
        minusButton.addActionListener(new GeometryPanelListener(
                "-",
                manuMethod,
                this,
                infoPanelCtr, geometryController));
        minusButton.setPreferredSize(new Dimension(50, 20));
        
        buttonPanel.add(plusButton);
        buttonPanel.add(minusButton);        
    }
    
    /**
     * To get a button of this panel.
     * @param identification title of the button.
     * @return the specified button.
     */
    public JButton getButton(String identification) {
        if(identification.equals("+")) {
            return plusButton;
        } else if(identification.equals("-")) {
            return minusButton;
        }
        return null;
    }
    
    /**
     * To remove a parameter panel set from this panel which is connected to a
     * geometry set.
     */
    public void removeGeometryParameterPanel() {        
        ArrayList<JPanel> panelsToRemove =
                parameterPanelsMap.get(geometryCount - 1);
        
        for(int i = 0; i < panelsToRemove.size(); i++) {
            parametersPanel.remove(panelsToRemove.get(i));            
        }
        geometryCount--;
        parameterPanelsMap.remove(geometryCount);
        textFieldsMap.remove(geometryCount);        
    }

    @Override
    /* (non-Javadoc)
     * @see ma.view.gui.ObserverInterface#update(ma.view.gui.ObserverInterface)
     */
    public void update() {        
        statusMessageLabel.setText(standardMessage + geometryCount);        
        this.revalidate();
    }
    
    
}
